var express = require('express'),
    router = express.Router(),
    mongoose = require('mongoose'),
    Test = mongoose.model('Test');


module.exports = function (app) {
  app.use('/test', router);
};

router.get('/', function (req, res, next) {
  res.send('test');
});

router.get('/view', function (req, res, next) { 
  res.send('kv'); 
});

router.get('/data', function (req, res, next) {  
  res.send(test);
});

router.get('/save', function (req, res, next) {  

  var test = new Test({
    blog: '51zovan'
  });
  test.save(function(err){
    if(err){      
      res.send('save err: ', err);
    }

    res.send('blog url:', test);
  })
});


var Post = mongoose.model('Post');
var User = mongoose.model('User');
var Category = mongoose.model('Category');

router.get('/kv', function (req, res, next) { 
  User.findOne(function (err, user) {
      if(err) console.log('err');
      Category.find(function (err, categories) {
        if (err) {
          return console.log('cannot find categories');
        }   
        categories.forEach(function (category) {
          for (var i = 0; i < 35; i++) {        
            var title = 'kv';
            var post = new Post({
              title: title,
              slug: title,
              content: 'kv889:' + i,
              category: category,
              author: user,
              published: true,
              meta: { favorites: 0 },
              comments: [ ],
              created: new Date
            });
            post.save(function (err, post) {
              console.log('saved post:', post.slug);
            });
          }
        })
        res.send(categories);
      })
  })
});